<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>历史试卷</title>
    <!--  引入核心皮肤-->
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!--   引入核心js-->
    <script src="../../layui/layui.js"></script>
    <script >

        layui.use(['layer','jquery','table','form'],function () {
            let layer=layui.layer;//定义组件
            let $=layui.jquery;//定义组件
            let table =layui.table;//定义表格组件
            let form=layui.form;//定义表格组件
            table.render({
                elem:'#dg',
                url:'../../question/selectAll',
                page:true,//开启分页
                parseData:function (res) {
                    return{
                        "code":0,
                        "msg":"",
                        "count":1000,
                        "data":res
                    }
                },
                cols: [[ //表头
                    {field: 'questionid', title: 'ID',type:'checkbox'}
                    ,{field: 'questionname', title: '考试名称',align:'center'}
                    ,{field:'startdate', width:200, title: '考试开始时间', sort: true, minWidth: 100, templet: "<div>{{layui.util.toDateString(d.startdate, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                    ,{field:'enddate', width:200, title: '考试开始时间', sort: true, minWidth: 100, templet: "<div>{{layui.util.toDateString(d.enddate, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                    ,{field: 'majorid', title: '考试专业',align:'center'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]],
                done: function(res, curr, count){
                    $(".layui-table-box").find("[data-field='id']").css("display","none");

                    $("[data-field='majorid']").children().each(function(){
                        if($(this).text()=='601'){
                            $(this).text("计算机专业")
                        }else if($(this).text()=='603'){
                            $(this).text("软件专业")
                        }else if($(this).text()=='602'){
                            $(this).text("食品安全专业")
                        }else if($(this).text()=='604'){
                            $(this).text("食品国际专业")
                        }else if($(this).text()=='605'){
                            $(this).text("生活专业")
                        }
                    });
                    pageCurr=curr;
                }
            });

            table.on('tool(dg)', function(obj){
                var data = obj.data;
                if(obj.event === 'yl'){
                    layer.open({
                        title: '预览试卷',
                        type: 2,
                        content: "myhquestion.html",
                        area: ['800px','800px'],
                        success: function (layero, index) {
                            // 获取子页面的iframe
                            var iframe = window['layui-layer-iframe' + index];
                            // 向子页面的全局函数child传参
                            iframe.child(data);
                        }
                    });
                } else if(obj.event === 'del'){
                    $.post('../../question/delete',{
                        questionId: data.questionid
                    },function (flag) {
                        console.log(data)
                        if(flag){
                            layer.msg('删除数据成功',{icon:6});
                            table.reload('dg');
                        }else{
                            layer.msg('删除数据失败',{icon:6});
                        }
                    })
                }
            });
        })

    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="yl">查看详情</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</head>
<body>
<table id="dg" lay-filter="dg"></table>
</body>
</html>